<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bounds-checking integer arithmetic: GLib Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GLib Reference Manual">
<link rel="up" href="glib-fundamentals.html" title="GLib Fundamentals">
<link rel="prev" href="glib-Byte-Order-Macros.html" title="Byte Order Macros">
<link rel="next" href="glib-Numerical-Definitions.html" title="Numerical Definitions">
<meta name="generator" content="GTK-Doc V1.24 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts">
<a href="#" class="shortcut">Top</a><span id="nav_description">  <span class="dim">|</span> 
                  <a href="#glib-Bounds-checked-integer-arithmetic.description" class="shortcut">Description</a></span>
</td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="glib-fundamentals.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="glib-Byte-Order-Macros.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="glib-Numerical-Definitions.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="glib-Bounds-checked-integer-arithmetic"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle"><a name="glib-Bounds-checked-integer-arithmetic.top_of_page"></a>Bounds-checking integer arithmetic</span></h2>
<p>Bounds-checking integer arithmetic — a set of helpers for performing checked integer arithmetic</p>
</td>
<td class="gallery_image" valign="top" align="right"></td>
</tr></table></div>
<div class="refsect1">
<a name="glib-Bounds-checked-integer-arithmetic.functions"></a><h2>Functions</h2>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="functions_return">
<col class="functions_name">
</colgroup>
<tbody>
<tr>
<td class="define_keyword">#define</td>
<td class="function_name">
<a class="link" href="glib-Bounds-checked-integer-arithmetic.html#g-uint-checked-add" title="g_uint_checked_add()">g_uint_checked_add</a><span class="c_punctuation">()</span>
</td>
</tr>
<tr>
<td class="define_keyword">#define</td>
<td class="function_name">
<a class="link" href="glib-Bounds-checked-integer-arithmetic.html#g-uint-checked-mul" title="g_uint_checked_mul()">g_uint_checked_mul</a><span class="c_punctuation">()</span>
</td>
</tr>
<tr>
<td class="define_keyword">#define</td>
<td class="function_name">
<a class="link" href="glib-Bounds-checked-integer-arithmetic.html#g-uint64-checked-add" title="g_uint64_checked_add()">g_uint64_checked_add</a><span class="c_punctuation">()</span>
</td>
</tr>
<tr>
<td class="define_keyword">#define</td>
<td class="function_name">
<a class="link" href="glib-Bounds-checked-integer-arithmetic.html#g-uint64-checked-mul" title="g_uint64_checked_mul()">g_uint64_checked_mul</a><span class="c_punctuation">()</span>
</td>
</tr>
<tr>
<td class="define_keyword">#define</td>
<td class="function_name">
<a class="link" href="glib-Bounds-checked-integer-arithmetic.html#g-size-checked-add" title="g_size_checked_add()">g_size_checked_add</a><span class="c_punctuation">()</span>
</td>
</tr>
<tr>
<td class="define_keyword">#define</td>
<td class="function_name">
<a class="link" href="glib-Bounds-checked-integer-arithmetic.html#g-size-checked-mul" title="g_size_checked_mul()">g_size_checked_mul</a><span class="c_punctuation">()</span>
</td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect1">
<a name="glib-Bounds-checked-integer-arithmetic.includes"></a><h2>Includes</h2>
<pre class="synopsis">#include &lt;glib.h&gt;
</pre>
</div>
<div class="refsect1">
<a name="glib-Bounds-checked-integer-arithmetic.description"></a><h2>Description</h2>
<p>GLib offers a set of macros for doing additions and multiplications
of unsigned integers, with checks for overflows.</p>
<p>The helpers all have three arguments.  A pointer to the destination
is always the first argument and the operands to the operation are
the other two.</p>
<p>Following standard GLib convention, the helpers return <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> in case
of success (ie: no overflow).</p>
<p>The helpers may be macros, normal functions or inlines.  They may be
implemented with inline assembly or compiler intrinsics where
available.</p>
</div>
<div class="refsect1">
<a name="glib-Bounds-checked-integer-arithmetic.functions_details"></a><h2>Functions</h2>
<div class="refsect2">
<a name="g-uint-checked-add"></a><h3>g_uint_checked_add()</h3>
<pre class="programlisting">#define             g_uint_checked_add(dest, a, b)</pre>
<p>Performs a checked addition of <em class="parameter"><code>a</code></em>
 and <em class="parameter"><code>b</code></em>
, storing the result in
<em class="parameter"><code>dest</code></em>
.</p>
<p>If the operation is successful, <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> is returned.  If the operation
overflows then the state of <em class="parameter"><code>dest</code></em>
 is undefined and <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
returned.</p>
<div class="refsect3">
<a name="id-1.3.7.6.2.6"></a><h4>Parameters</h4>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="parameters_name">
<col class="parameters_description">
<col width="200px" class="parameters_annotations">
</colgroup>
<tbody>
<tr>
<td class="parameter_name"><p>dest</p></td>
<td class="parameter_description"><p>a pointer to the <a class="link" href="glib-Basic-Types.html#guint" title="guint ()"><span class="type">guint</span></a> destination</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>a</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint" title="guint ()"><span class="type">guint</span></a> left operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>b</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint" title="guint ()"><span class="type">guint</span></a> right operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect3">
<a name="id-1.3.7.6.2.7"></a><h4>Returns</h4>
<p> <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if there was no overflow</p>
</div>
<p class="since">Since: <a class="link" href="api-index-2-48.html#api-index-2.48">2.48</a></p>
</div>
<hr>
<div class="refsect2">
<a name="g-uint-checked-mul"></a><h3>g_uint_checked_mul()</h3>
<pre class="programlisting">#define             g_uint_checked_mul(dest, a, b)</pre>
<p>Performs a checked multiplication of <em class="parameter"><code>a</code></em>
 and <em class="parameter"><code>b</code></em>
, storing the result in
<em class="parameter"><code>dest</code></em>
.</p>
<p>If the operation is successful, <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> is returned.  If the operation
overflows then the state of <em class="parameter"><code>dest</code></em>
 is undefined and <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
returned.</p>
<div class="refsect3">
<a name="id-1.3.7.6.3.6"></a><h4>Parameters</h4>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="parameters_name">
<col class="parameters_description">
<col width="200px" class="parameters_annotations">
</colgroup>
<tbody>
<tr>
<td class="parameter_name"><p>dest</p></td>
<td class="parameter_description"><p>a pointer to the <a class="link" href="glib-Basic-Types.html#guint" title="guint ()"><span class="type">guint</span></a> destination</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>a</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint" title="guint ()"><span class="type">guint</span></a> left operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>b</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint" title="guint ()"><span class="type">guint</span></a> right operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect3">
<a name="id-1.3.7.6.3.7"></a><h4>Returns</h4>
<p> <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if there was no overflow</p>
</div>
<p class="since">Since: <a class="link" href="api-index-2-48.html#api-index-2.48">2.48</a></p>
</div>
<hr>
<div class="refsect2">
<a name="g-uint64-checked-add"></a><h3>g_uint64_checked_add()</h3>
<pre class="programlisting">#define             g_uint64_checked_add(dest, a, b)</pre>
<p>Performs a checked addition of <em class="parameter"><code>a</code></em>
 and <em class="parameter"><code>b</code></em>
, storing the result in
<em class="parameter"><code>dest</code></em>
.</p>
<p>If the operation is successful, <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> is returned.  If the operation
overflows then the state of <em class="parameter"><code>dest</code></em>
 is undefined and <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
returned.</p>
<div class="refsect3">
<a name="id-1.3.7.6.4.6"></a><h4>Parameters</h4>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="parameters_name">
<col class="parameters_description">
<col width="200px" class="parameters_annotations">
</colgroup>
<tbody>
<tr>
<td class="parameter_name"><p>dest</p></td>
<td class="parameter_description"><p>a pointer to the <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a> destination</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>a</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a> left operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>b</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a> right operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect3">
<a name="id-1.3.7.6.4.7"></a><h4>Returns</h4>
<p> <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if there was no overflow</p>
</div>
<p class="since">Since: <a class="link" href="api-index-2-48.html#api-index-2.48">2.48</a></p>
</div>
<hr>
<div class="refsect2">
<a name="g-uint64-checked-mul"></a><h3>g_uint64_checked_mul()</h3>
<pre class="programlisting">#define             g_uint64_checked_mul(dest, a, b)</pre>
<p>Performs a checked multiplication of <em class="parameter"><code>a</code></em>
 and <em class="parameter"><code>b</code></em>
, storing the result in
<em class="parameter"><code>dest</code></em>
.</p>
<p>If the operation is successful, <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> is returned.  If the operation
overflows then the state of <em class="parameter"><code>dest</code></em>
 is undefined and <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
returned.</p>
<div class="refsect3">
<a name="id-1.3.7.6.5.6"></a><h4>Parameters</h4>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="parameters_name">
<col class="parameters_description">
<col width="200px" class="parameters_annotations">
</colgroup>
<tbody>
<tr>
<td class="parameter_name"><p>dest</p></td>
<td class="parameter_description"><p>a pointer to the <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a> destination</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>a</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a> left operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>b</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#guint64" title="guint64"><span class="type">guint64</span></a> right operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect3">
<a name="id-1.3.7.6.5.7"></a><h4>Returns</h4>
<p> <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if there was no overflow</p>
</div>
<p class="since">Since: <a class="link" href="api-index-2-48.html#api-index-2.48">2.48</a></p>
</div>
<hr>
<div class="refsect2">
<a name="g-size-checked-add"></a><h3>g_size_checked_add()</h3>
<pre class="programlisting">#define             g_size_checked_add(dest, a, b)</pre>
<p>Performs a checked addition of <em class="parameter"><code>a</code></em>
 and <em class="parameter"><code>b</code></em>
, storing the result in
<em class="parameter"><code>dest</code></em>
.</p>
<p>If the operation is successful, <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> is returned.  If the operation
overflows then the state of <em class="parameter"><code>dest</code></em>
 is undefined and <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
returned.</p>
<div class="refsect3">
<a name="id-1.3.7.6.6.6"></a><h4>Parameters</h4>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="parameters_name">
<col class="parameters_description">
<col width="200px" class="parameters_annotations">
</colgroup>
<tbody>
<tr>
<td class="parameter_name"><p>dest</p></td>
<td class="parameter_description"><p>a pointer to the <a class="link" href="glib-Basic-Types.html#gsize" title="gsize"><span class="type">gsize</span></a> destination</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>a</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#gsize" title="gsize"><span class="type">gsize</span></a> left operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>b</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#gsize" title="gsize"><span class="type">gsize</span></a> right operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect3">
<a name="id-1.3.7.6.6.7"></a><h4>Returns</h4>
<p> <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if there was no overflow</p>
</div>
<p class="since">Since: <a class="link" href="api-index-2-48.html#api-index-2.48">2.48</a></p>
</div>
<hr>
<div class="refsect2">
<a name="g-size-checked-mul"></a><h3>g_size_checked_mul()</h3>
<pre class="programlisting">#define             g_size_checked_mul(dest, a, b)</pre>
<p>Performs a checked multiplication of <em class="parameter"><code>a</code></em>
 and <em class="parameter"><code>b</code></em>
, storing the result in
<em class="parameter"><code>dest</code></em>
.</p>
<p>If the operation is successful, <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> is returned.  If the operation
overflows then the state of <em class="parameter"><code>dest</code></em>
 is undefined and <a class="link" href="glib-Standard-Macros.html#FALSE:CAPS" title="FALSE"><code class="literal">FALSE</code></a> is
returned.</p>
<div class="refsect3">
<a name="id-1.3.7.6.7.6"></a><h4>Parameters</h4>
<div class="informaltable"><table class="informaltable" width="100%" border="0">
<colgroup>
<col width="150px" class="parameters_name">
<col class="parameters_description">
<col width="200px" class="parameters_annotations">
</colgroup>
<tbody>
<tr>
<td class="parameter_name"><p>dest</p></td>
<td class="parameter_description"><p>a pointer to the <a class="link" href="glib-Basic-Types.html#gsize" title="gsize"><span class="type">gsize</span></a> destination</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>a</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#gsize" title="gsize"><span class="type">gsize</span></a> left operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
<tr>
<td class="parameter_name"><p>b</p></td>
<td class="parameter_description"><p>the <a class="link" href="glib-Basic-Types.html#gsize" title="gsize"><span class="type">gsize</span></a> right operand</p></td>
<td class="parameter_annotations"> </td>
</tr>
</tbody>
</table></div>
</div>
<div class="refsect3">
<a name="id-1.3.7.6.7.7"></a><h4>Returns</h4>
<p> <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS" title="TRUE"><code class="literal">TRUE</code></a> if there was no overflow</p>
</div>
<p class="since">Since: <a class="link" href="api-index-2-48.html#api-index-2.48">2.48</a></p>
</div>
</div>
<div class="refsect1">
<a name="glib-Bounds-checked-integer-arithmetic.other_details"></a><h2>Types and Values</h2>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.24</div>
</body>
</html>